package luogu.GraphTheory;
//P1546 [USACO3.1]最短网络 Agri-Net

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author :chenjie
 * @date :Created 2023/1/8 14:52
 */
public class TheShortestNetworkAgriNet {
    static int[][]arr=new int[110][110];
    static int n,sum;
    static int[]dist=new int[110];
    static boolean[]st=new boolean[110];
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                arr[i][j]=sc.nextInt();
            }
        }
        prim();
        System.out.println(sum);

    }
    public static void prim(){
        Arrays.fill(dist,0x3f3f3f3f);
        for (int i = 0; i < n; i++) {
            int t=-1;
            for (int j = 1; j <= n; j++) {
                if(!st[j]&&(t==-1||dist[t]>dist[j])){
                    t=j;
                }
            }
            if(i>0){
                sum=sum+dist[t];
            }
            for (int j = 1; j <= n; j++) {
                dist[j]=Math.min(dist[j],arr[t][j]);
            }
            st[t]=true;
        }
    }
}
